//
//  WBTitleButton.swift
//  WeiboSwift
//
//  Created by hfwd on 2017/6/1.
//  Copyright © 2017年 hfwd. All rights reserved.
//

import UIKit

class WBTitleButton: UIButton {

    /// 重载构造函数
    ///
    /// - Parameter title: nil显示首页，否则显示箭头和名称
    init(title: String?) {
        super.init(frame: CGRect())
        
        if title == nil {
            
            setTitle("首页", for: .normal)
            
        } else {
            
            setTitle(title! + "  ", for: .normal)
            
            setImage(UIImage(named: "navi_downarrow"), for: .normal)
            
            setImage(UIImage(named: "navi_uparrow"), for: .selected)
        }
        
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        
        setTitleColor(UIColor.blue, for: .normal)
        
        sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let titleLabel = titleLabel,
              let imageView = imageView
        else {
            return
        }
        
//        titleLabel.frame = titleLabel.frame.offsetBy(dx: -imageView.bounds.width, dy: 0)
//
//        imageView.frame = imageView.frame.offsetBy(dx: titleLabel.bounds.width, dy: 0)
        
        titleLabel.frame.origin.x = 0
        imageView.frame.origin.x = titleLabel.frame.size.width
        
    }
}
